﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HousingInfo.Library;

namespace WebHousingInfo
{
    public partial class BinhLuanDeTai : System.Web.UI.Page
    {
        private static string mode_edit_comment;
        private static string mode_add_comment;
        private static string mode_quote_comment;
        private static string mode_quote_forum;
        private static string mode_back;
        private static ForumCommentBO forumcommentbo;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["UserName"] == null)
                    Response.Redirect("DangNhap.aspx");
                mode_edit_comment = Request.QueryString["id"];
                mode_add_comment = Request.QueryString["idforum"];
                mode_quote_comment = Request.QueryString["qoutecomment"];
                mode_quote_forum = Request.QueryString["qouteforum"];
                mode_back = Request.QueryString["cate"];
                if (mode_edit_comment != null)
                {
                    forumcommentbo = ForumCommentBO.GetForumCommentBO(Convert.ToInt32(mode_edit_comment));
                    txttieude.Text = forumcommentbo.ForumCommentTitle;
                    txtnoidung.Text = forumcommentbo.CommentContent;
                }
                else if (mode_add_comment != null)
                {
                    if (mode_quote_comment != null)
                    {
                        //thuc hien trich cho comment
                        txtnoidung.Text = "[Trich]" + getContent_DeleteQuote(ForumCommentBO.GetForumCommentBO(Convert.ToInt32(mode_quote_comment)).CommentContent) + "[/Trich]";
                    }
                    else if (mode_quote_forum != null)
                    {
                        txtnoidung.Text = "[Trich]" + getContent_DeleteQuote(ForumNewsBO.GetForumNewsBO(Convert.ToInt32(mode_quote_forum)).ForumNewsContent) + "[/Trich]";
                    }
                }
            }
        }

        protected void btndangtin_Click(object sender, EventArgs e)
        {
            if (Session["UserName"] == null)
                Response.Redirect("DangNhap.aspx");
            if (mode_edit_comment != null)
            {
                int forumcommentid = Convert.ToInt32(mode_edit_comment);
                forumcommentbo = ForumCommentBO.GetForumCommentBO(forumcommentid);
                forumcommentbo.ForumCommentTitle = txttieude.Text;
                forumcommentbo.CommentContent = txtnoidung.Text;
                forumcommentbo.UpdatedDate = DateTime.Now;
                try
                {
                    forumcommentbo.Save();
                    Response.Redirect("DienDanBinhLuan.aspx?categoryid=" + mode_back + "&id=" + forumcommentbo.ForumNewID);
                }
                catch
                {
                    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Success", "alert('Cập nhật thất bại!')", true);
                    return;
                }
            }
            else if (mode_add_comment != null)
            {
                if (mode_quote_comment != null)
                {
                    //thuc hien trich cho comment
                    forumcommentbo = ForumCommentBO.NewForumCommentBO();
                    forumcommentbo.ForumNewID = Convert.ToInt32(mode_add_comment);
                    forumcommentbo.UserName = Session["UserName"].ToString();
                    forumcommentbo.ForumCommentTitle = txttieude.Text;
                    try
                    {
                        int index = txtnoidung.Text.IndexOf("[/Trich]");
                        if (index == -1)
                        {
                            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Success", "alert('Không được thay đổi các thẻ trích dẫn')", true);
                            txtnoidung.Text = "[Trich]" + ForumCommentBO.GetForumCommentBO(Convert.ToInt32(mode_quote_comment)).CommentContent + "[/Trich]";
                            return;
                        }
                        int leng = txtnoidung.Text.Length;
                        //string content = txtnoidung.Text.Substring(0, index+8);
                        string content = txtnoidung.Text.Substring(index + 8, leng - index - 8);
                        index = content.IndexOf("</p>");
                        leng = content.Length;
                        content = content.Substring(0, index) + content.Substring(index + 4, leng - index - 4);
                        forumcommentbo.CommentContent = "<blockquote_bydiendanhadat><table cellspacing='0' cellpadding='6' border='0' width='95%' style='border: 1px dashed rgb(255, 156, 0); margin: 10px; width: 95%;'><tbody><tr><td class='qoute_admin'></td></tr><tr><td><div style='width: 95%;'>Nguyên văn bởi <strong>" + ForumCommentBO.GetForumCommentBO(Convert.ToInt32(mode_quote_comment)).UserName + "</strong><a rel='nofollow' href='#'><img border='0' alt='View Post' src='img/viewpost.gif' class='inlineimg' title='View Post'></a></div><div style='font-style: italic;'>" + getContent_DeleteQuote(ForumCommentBO.GetForumCommentBO(Convert.ToInt32(mode_quote_comment)).CommentContent) + "</div></td></tr></tbody></table></blockquote_bydiendanhadat>" + content;
                    }
                    catch
                    {
                        forumcommentbo.CommentContent = txtnoidung.Text;
                    }
                    forumcommentbo.CreatedDate = DateTime.Now;
                    forumcommentbo.UpdatedDate = DateTime.Now;
                    try
                    {
                        forumcommentbo.Save();
                        Response.Redirect("DienDanBinhLuan.aspx?categoryid=" + mode_back + "&id=" + mode_add_comment);
                    }
                    catch
                    {
                        ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Success", "alert('Có lỗi khi đăng bình luận. Xin hãy thử lại !!!')", true);
                        return;
                    }

                }
                    else if (mode_quote_forum != null)
                    {
                        //thuc hien trich cho forum
                        forumcommentbo = ForumCommentBO.NewForumCommentBO();
                        forumcommentbo.ForumNewID = Convert.ToInt32(mode_add_comment);
                        forumcommentbo.UserName = Session["UserName"].ToString();
                        forumcommentbo.ForumCommentTitle = txttieude.Text;
                        try
                        {
                            int index = txtnoidung.Text.IndexOf("[/Trich]");
                            if (index == -1)
                            {
                                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Success", "alert('Không được thay đổi các thẻ trích dẫn')", true);
                                txtnoidung.Text = "[Trich]" + ForumNewsBO.GetForumNewsBO(Convert.ToInt32(mode_quote_forum)).ForumNewsContent + "[/Trich]";
                                return;
                            }
                            int leng = txtnoidung.Text.Length;
                            //string content = txtnoidung.Text.Substring(0, index+8);
                            string content = txtnoidung.Text.Substring(index + 8, leng - index - 8);
                            index = content.IndexOf("</p>");
                            leng = content.Length;
                            content = content.Substring(0, index) + content.Substring(index + 4, leng - index - 4);
                            forumcommentbo.CommentContent = "<blockquote_bydiendanhadat><table cellspacing='0' cellpadding='6' border='0' width='95%' style='border: 1px dashed rgb(255, 156, 0); margin: 10px; width: 95%;'><tbody><tr><td class='qoute_admin'></td></tr><tr><td><div style='width: 95%;'>Nguyên văn bởi <strong>" + ForumNewsBO.GetForumNewsBO(Convert.ToInt32(mode_quote_forum)).UserName + "</strong><a rel='nofollow' href='#'><img border='0' alt='View Post' src='img/viewpost.gif' class='inlineimg' title='View Post'></a></div><div style='font-style: italic;'>" + getContent_DeleteQuote(ForumNewsBO.GetForumNewsBO(Convert.ToInt32(mode_quote_forum)).ForumNewsContent) + "</div></td></tr></tbody></table></blockquote_bydiendanhadat>" + content;
                        }
                        catch
                        {
                            forumcommentbo.CommentContent = txtnoidung.Text;
                        }
                        forumcommentbo.CreatedDate = DateTime.Now;
                        forumcommentbo.UpdatedDate = DateTime.Now;
                        try
                        {
                            forumcommentbo.Save();
                            Response.Redirect("DienDanBinhLuan.aspx?categoryid=" + mode_back + "&id=" + mode_add_comment);
                        }
                        catch
                        {
                            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Success", "alert('Có lỗi khi đăng bình luận. Xin hãy thử lại !!!')", true);
                            return;
                        }
                    }
                else
                {
                    forumcommentbo = ForumCommentBO.NewForumCommentBO();
                    forumcommentbo.ForumNewID = Convert.ToInt32(mode_add_comment);
                    forumcommentbo.UserName = Session["UserName"].ToString();
                    forumcommentbo.ForumCommentTitle = txttieude.Text;
                    forumcommentbo.CommentContent = txtnoidung.Text;
                    forumcommentbo.CreatedDate = DateTime.Now;
                    forumcommentbo.UpdatedDate = DateTime.Now;

                    try
                    {
                        forumcommentbo.Save();
                        Response.Redirect("DienDanBinhLuan.aspx?categoryid=" + mode_back + "&id=" + mode_add_comment);
                    }
                    catch
                    {
                        ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Success", "alert('Có lỗi khi đăng bình luận. Xin hãy thử lại !!!')", true);
                        return;
                    }
                }

            }
        }

        protected void btnquaylai_Click(object sender, EventArgs e)
        {

        }

        private string getContent_DeleteQuote(string content)
        {
            try
            {
                int getindex = content.IndexOf("</blockquote_bydiendanhadat>");
                if (getindex == -1)
                {
                    return content;
                }
                else
                {
                    int getleng = content.Length;
                    return content.Substring(getindex + 28, getleng - getindex - 28);
                }
            }
            catch
            {
                return content;

            }
        }
    }
}
